package org.hackystat.devcathlon.ui.page.scoreboard.halloffame;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;
import org.hackystat.devcathlon.engine.team.Team;
import org.hackystat.devcathlon.engine.user.User;
import org.hackystat.devcathlon.ui.DevcathlonSession;

/**
 * Display the developer with most points events to the Hall of Fame.
 * 
 * @author John Ancheta, Scheller Sanchez
 */
public class MostDeveloperPointsPanel extends Panel {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * This Panel displays the developer with the most points for Hall of Fame.
   * 
   * @param id The Wicket id from the html markup.
   */
  public MostDeveloperPointsPanel(String id) {
    super(id);
    
    final HashMap<String, Integer> mapMostDeveloperPoints = new HashMap<String, Integer>();
    Collection<Match> matches = MatchManager.getInstance().getAllMatches();
    
    for (Match match : matches) {
      Set<Team> teams = match.getTeams();
      for (Team team : teams) {
        for (User user : team.getMembers()) {
          int currentUserPoints = match.getTotalScore(user);
          if (mapMostDeveloperPoints.containsKey(user.getEmail())) {
            Integer points = mapMostDeveloperPoints.get(user.getEmail());
            currentUserPoints += points.intValue();
          }
          mapMostDeveloperPoints.put(user.getFirstName() + " " + user.getLastName(),
                                     Integer.valueOf(currentUserPoints));
        }
        User owner = team.getOwner();
        int currentUserPoints = match.getTotalScore(owner);
        if (mapMostDeveloperPoints.containsKey(owner.getEmail())) {
        Integer points = mapMostDeveloperPoints.get(owner.getEmail());
          currentUserPoints += points.intValue();
        }
        mapMostDeveloperPoints.put(owner.getFirstName() + " " + owner.getLastName(), 
                                   Integer.valueOf(currentUserPoints));
      }
    }
    
    final List<String> listMostDeveloperPoints = new ArrayList<String>();
    Set<Map.Entry<String, Integer>> mapentryMapMostDeveloperPoints = 
      mapMostDeveloperPoints.entrySet();
    
    int rank = 0; // The rank of the event.
    
    while (!mapentryMapMostDeveloperPoints.isEmpty()) {
      Map.Entry<String, Integer> most = null;
      for (Map.Entry<String,Integer> mapentry : mapentryMapMostDeveloperPoints) {
        if (most == null) {
          most = mapentry;
        }
        if (most.getValue().intValue() < mapentry.getValue().intValue()) {
          most = mapentry;
        }
      }
      rank++;
      listMostDeveloperPoints.add(rank + ".  " + most.getKey() + " (" + 
          most.getValue().toString() + " count)");
      if (DevcathlonSession.get().getEmail() == null) {
        mapentryMapMostDeveloperPoints.clear();
      }
      else {
        mapentryMapMostDeveloperPoints.remove(most);
      }
    }
    
    DataView dataview = new DataView("ItemList", new ListDataProvider(listMostDeveloperPoints)) {
      private static final long serialVersionUID = 1L;
      
      @Override
      protected void populateItem(Item item) {
        final String developer = (String) item.getModelObject();
        item.add(new Label("labelMostDeveloperPoints", developer));
      }
    };
    add(dataview);    
  }
}
